package com.sxtyxt.online.file;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sxtyxt.entity.FileDetails;
import com.sxtyxt.entity.ResultVo;
import com.sxtyxt.online.ActBase;
import com.sxtyxt.online.ImageViewPager;
import com.sxtyxt.online.R;
import com.sxtyxt.online.SystemConst;
import com.sxtyxt.util.GsonUtil;
import com.sxtyxt.util.ImageLoaderUtil;
import com.sxtyxt.volley.LoadingDialogResultListenerImpl;
import com.sxtyxt.volley.URL;
import com.sxtyxt.volley.VolleyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/11.
 */

public class ActFileDetail extends ActBase implements View.OnClickListener {


    private TextView tvLogisticsId, tvEndPersonName, tvEndPersonTelephone, tvReceiveDistrict, tvEndPersonAddress;
    private TextView tvStartPersonName, tvStartPersonTelephone, tvSenderDistrict, tvStartPersonAddress;
    private TextView tvGoods;
    private ImageView ivGoodsPic, ivBoxPic, ivStartPersonIdCard;


    @Override
    protected void initUI() {
        super.initUI();

        tvLogisticsId = (TextView) findViewById(R.id.tv_logisticsId);
        tvEndPersonName = (TextView) findViewById(R.id.tv_endPerson_name);
        tvEndPersonTelephone = (TextView) findViewById(R.id.tv_endPerson_telephone);
        tvReceiveDistrict = (TextView) findViewById(R.id.tv_receive_district);
        tvEndPersonAddress = (TextView) findViewById(R.id.tv_endPerson_address);

        tvStartPersonName = (TextView) findViewById(R.id.tv_startPerson_name);
        tvStartPersonTelephone = (TextView) findViewById(R.id.tv_startPerson_telephone);
        tvSenderDistrict = (TextView) findViewById(R.id.tv_sender_district);
        tvStartPersonAddress = (TextView) findViewById(R.id.tv_startPerson_address);

        tvGoods = (TextView) findViewById(R.id.tv_goods);

        ivGoodsPic = (ImageView) findViewById(R.id.iv_goods_pic);
        ivGoodsPic.setOnClickListener(this);
        ivBoxPic = (ImageView) findViewById(R.id.iv_box_pic);
        ivBoxPic.setOnClickListener(this);
        ivStartPersonIdCard = (ImageView) findViewById(R.id.iv_startPerson_idCard);
        ivStartPersonIdCard.setOnClickListener(this);

        String fileId = getIntent().getStringExtra("fileId");
        getFileDetails(fileId);

    }

    private void getFileDetails(String fileId) {
        VolleyUtils.requestService(SystemConst.FILE_DETAILS, URL.getFileDetails(fileId)
                , new LoadingDialogResultListenerImpl(this, "正在加载数据") {
                    @Override
                    public void onSuccess(String response) {
                        super.onSuccess(response);
                        FileDetailsVo vo = GsonUtil.deser(response, FileDetailsVo.class);
                        if (vo == null) {
                            return;
                        }
                        if (vo.getResult() == 1) {

                            if (vo.getList() != null && vo.getList().size() > 0) {

                                FileDetails details = vo.getList().get(0);
                                if (details != null) {
                                    tvLogisticsId.setText(details.getLogisticsId());
                                    tvEndPersonName.setText(details.getEndPersonName());
                                    tvEndPersonTelephone.setText(details.getEndPersonTelephone());
                                    tvEndPersonAddress.setText(details.getEndPersonAddress());
                                    tvReceiveDistrict.setText(details.getReceiveDistrict());
                                    tvStartPersonName.setText(details.getStartPersonName());
                                    tvStartPersonTelephone.setText(details.getStartPersonTelephone());
                                    tvStartPersonAddress.setText(details.getStartPersonAddress());
                                    tvSenderDistrict.setText(details.getSenderDistrict());
                                    tvGoods.setText(details.getGoods());

                                    pkId = details.getPkId();

                                    ImageLoader.getInstance().displayImage(SystemConst.DEFAULT_IMAGE_URL + "/" + details.getPkId() + "/1", ivGoodsPic, ImageLoaderUtil.getOptionCustom(R.drawable.icon_goods));
                                    ImageLoader.getInstance().displayImage(SystemConst.DEFAULT_IMAGE_URL + "/" + details.getPkId() + "/2", ivBoxPic, ImageLoaderUtil.getOptionCustom(R.drawable.icon_box));
                                    ImageLoader.getInstance().displayImage(SystemConst.DEFAULT_IMAGE_URL + "/" + details.getPkId() + "/3", ivStartPersonIdCard, ImageLoaderUtil.getOptionCustom(R.drawable.icon_idcard));
                                }
                            }

                        }

                    }

                    @Override
                    public void onError() {
                        super.onError();
                    }
                });


    }


    private String pkId;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_goods_pic:
                List<String> imageList = new ArrayList<String>();
                imageList.add("/" + pkId + "/1");
                ImageViewPager.setImageList(imageList);
                ImageViewPager.setCurrentIndex(0);
                Intent intent = new Intent(this, ImageViewPager.class);
                this.startActivity(intent);
                break;
            case R.id.iv_box_pic:
                imageList = new ArrayList<String>();
                imageList.add("/" + pkId + "/2");
                ImageViewPager.setImageList(imageList);
                ImageViewPager.setCurrentIndex(0);
                intent = new Intent(this, ImageViewPager.class);
                this.startActivity(intent);
                break;
            case R.id.iv_startPerson_idCard:
                imageList = new ArrayList<String>();
                imageList.add("/" + pkId + "/3");
                ImageViewPager.setImageList(imageList);
                ImageViewPager.setCurrentIndex(0);
                intent = new Intent(this, ImageViewPager.class);
                this.startActivity(intent);
                break;
        }
    }


    class FileDetailsVo extends ResultVo<FileDetails> {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.file_details;
    }
}
